function iris_analysis() 
%iris_sqhingeloss(); 
%iris_smhingeloss(); 
%iris_logloss();
iris_scale();

end

function iris_sqloss()
% load iris

% $$$ w = importdata('test.iris.sqloss.debug');
% $$$ for i=1:size(iris,2) 
% $$$     iris(:,i) = iris(:,i) / norm(iris(:,i),2);
% $$$ end
% $$$ 
% $$$ y = iris*w(1,1:end-1)'+w(1,end);
% $$$ I = find(w(1,1:end-1) ~= 0);
% $$$ x = -w(1,end) / w(1, I);
% $$$ label = cellfun(@(x) (strcmp(x,'Iris-setosa'))*(-1) + ...
% $$$                 (~strcmp(x,'Iris-setosa'))*(1), iris_label );
% $$$ N = find(label < 0);
% $$$ P = find(label > 0);
% $$$ 
% $$$ h = clf;
% $$$ hold on
% $$$ plot(iris(N, I), ones(size(y(N))), '+', iris(P, I), ...
% $$$        ones(size(y(P))), 'o');
% $$$ legend('setosa', 'non-setosa')
% $$$ line([x x],ylim,'Color',[1 0 0])
% $$$ xlabel('Petal width')
% $$$ print(h, '-depsc', 'iris_1.eps');


% $$$ output = iris*w(2,1:end-1)'+w(2,end);
% $$$ I = find(w(2,1:end-1) ~= 0);
% $$$ label = cellfun(@(x) (strcmp(x,'Iris-setosa'))*(-1) + ...
% $$$                 (~strcmp(x,'Iris-setosa'))*(1), iris_label );
% $$$ error = find(label ~= sign(output))
% $$$ N = find(label < 0);
% $$$ P = find(label > 0);
% $$$ 
% $$$ 
% $$$ xrange = [0 0.15];
% $$$ yrange = [0 0.2];
% $$$ inc = 0.01;
% $$$ [x, y] = meshgrid(xrange(1):inc:xrange(2), yrange(1):inc:yrange(2));
% $$$ image_size = size(x);
% $$$ xy = [x(:) y(:)];
% $$$ f = xy*w(2,I)'+w(2,end);
% $$$ idx = (f < 0)*(1) + (f >=0) * 2;
% $$$ decisionmap = reshape(idx, image_size);
% $$$ 
% $$$ h = clf;
% $$$ imagesc(xrange,yrange,decisionmap);
% $$$ hold on;
% $$$ set(gca,'ydir','normal');
% $$$ 
% $$$ cmap = [1 0.8 0.8; 0.95 1 0.95; 0.9 0.9 1];
% $$$ colormap(cmap);
% $$$ 
% $$$ plot(iris(N, I(1)), iris(N, I(2)), '+', iris(P, I(1)), ...
% $$$        iris(P, I(2)), 'o');
% $$$ legend('setosa', 'non-setosa', 'Location','NorthOutside','Orientation',...
% $$$        'Horizontal')
% $$$ %line(xlim, ylim,'Color',[1 0 0])
% $$$ xlabel('Petal length')
% $$$ ylabel('Petal width')
% $$$ 
% $$$ hold on;
% $$$ print(h, '-depsc', 'iris_2.eps');
end

function iris_logloss()
load iris
iris = normalize_std1(iris);
w = importdata('test.iris.log.debug');

y = iris*w(1,1:end-1)'+w(1,end);
I = find(w(1,1:end-1) ~= 0);
x = -w(1,end) / w(1, I);
label = cellfun(@(x) (strcmp(x,'Iris-setosa'))*(-1) + ...
                 (~strcmp(x,'Iris-setosa'))*(1), iris_label );
N = find(label < 0);
P = find(label > 0);
 
h = clf;
hold on
plot(iris(N, I), ones(size(y(N))), '+', iris(P, I), ...
       ones(size(y(P))), 'o');
legend('setosa', 'non-setosa')
line([x x],ylim,'Color',[1 0 0])
xlabel('Petal length')
print(h, '-depsc', 'iris_logloss_1.eps');
print(h, '-dpdf', 'iris_logloss_1.pdf');

output = iris*w(2,1:end-1)'+w(2,end);
I = [3 4];
label = cellfun(@(x) (strcmp(x,'Iris-setosa'))*(-1) + ...
                (~strcmp(x,'Iris-setosa'))*(1), iris_label );
error = find(label ~= sign(output))
N = find(label < 0);
P = find(label > 0);


xrange = [floor(min(iris(:,I(1)))) ceil(max(iris(:,I(1))))];
yrange = [floor(min(iris(:,I(2)))) ceil(max(iris(:,I(2))))];
inc = 0.01;
[x, y] = meshgrid(xrange(1):inc:xrange(2), yrange(1):inc:yrange(2));
image_size = size(x);
xy = [x(:) y(:)];
f = xy*w(2,I)'+w(2,end);
idx = (f < 0)*(1) + (f >=0) * 2;
decisionmap = reshape(idx, image_size);

h = clf;
imagesc(xrange,yrange,decisionmap);
hold on;
set(gca,'ydir','normal');

cmap = [1 0.8 0.8; 0.95 1 0.95; 0.9 0.9 1];
colormap(cmap);

plot(iris(N, I(1)), iris(N, I(2)), '+', iris(P, I(1)), ...
       iris(P, I(2)), 'o');
legend('setosa', 'non-setosa', 'Location','NorthOutside','Orientation',...
       'Horizontal')
%line(xlim, ylim,'Color',[1 0 0])
xlabel('Petal length')
ylabel('Petal width')

hold on;
print(h, '-depsc', 'iris_logloss_2.eps');
print(h, '-dpdf', 'iris_logloss_2.pdf');
end

function iris_sqhingeloss()

load iris
iris = normalize_std1(iris);
w = importdata('test.iris.sqhinge.debug');

y = iris*w(1,1:end-1)'+w(1,end);
I = find(w(1,1:end-1) ~= 0);
x = -w(1,end) / w(1, I);
label = cellfun(@(x) (strcmp(x,'Iris-setosa'))*(-1) + ...
                 (~strcmp(x,'Iris-setosa'))*(1), iris_label );
N = find(label < 0);
P = find(label > 0);

h = clf;
hold on
plot(iris(N, I), ones(size(y(N))), '+', iris(P, I), ...
        ones(size(y(P))), 'o');
legend('setosa', 'non-setosa')
line([x x],ylim,'Color',[1 0 0])
xlabel('Petal length')
title('Squared hingeloss');
set(gca, 'YTick',[]);
print(h, '-depsc', 'iris_sqhingeloss_1.eps');
print(h, '-dpdf', 'iris_sqhingeloss_1.pdf');

%%%%%%%%%%%%%%%

output = iris*w(2,1:end-1)'+w(2,end);
I = find(w(2,1:end-1) ~= 0);
label = cellfun(@(x) (strcmp(x,'Iris-setosa'))*(-1) + ...
                (~strcmp(x,'Iris-setosa'))*(1), iris_label );
error = find(label ~= sign(output))
N = find(label < 0);
P = find(label > 0);


xrange = [floor(min(iris(:,I(1)))) ceil(max(iris(:,I(1))))];
yrange = [floor(min(iris(:,I(2)))) ceil(max(iris(:,I(2))))];
inc = 0.01;
[x, y] = meshgrid(xrange(1):inc:xrange(2), yrange(1):inc:yrange(2));
image_size = size(x);
xy = [x(:) y(:)];
f = xy*w(2,I)'+w(2,end);
idx = (f < 0)*(1) + (f >=0) * 2;
decisionmap = reshape(idx, image_size);

h = clf;
imagesc(xrange,yrange,decisionmap);
hold on;
set(gca,'ydir','normal');

cmap = [1 0.8 0.8; 0.95 1 0.95; 0.9 0.9 1];
colormap(cmap);

plot(iris(N, I(1)), iris(N, I(2)), '+', iris(P, I(1)), ...
       iris(P, I(2)), 'o');
legend('setosa', 'non-setosa', 'Location','NorthOutside','Orientation',...
       'Horizontal')
%line(xlim, ylim,'Color',[1 0 0])
ylabel('Petal width')
xlabel('Petal length')

hold on;
print(h, '-depsc', 'iris_sqhingeloss_2.eps');
print(h, '-dpdf', 'iris_sqhingeloss_2.pdf');
end

function iris_smhingeloss()

load iris
iris = normalize_std1(iris);
w = importdata('test.iris.smhinge.debug');

y = iris*w(1,1:end-1)'+w(1,end);
I = find(w(1,1:end-1) ~= 0);
x = -w(1,end) / w(1, I);
label = cellfun(@(x) (strcmp(x,'Iris-setosa'))*(-1) + ...
                 (~strcmp(x,'Iris-setosa'))*(1), iris_label );
N = find(label < 0);
P = find(label > 0);

h = clf;
hold on
plot(iris(N, I), ones(size(y(N))), '+', iris(P, I), ...
        ones(size(y(P))), 'o');
legend('setosa', 'non-setosa')
line([x x],ylim,'Color',[1 0 0])
xlabel('Petal length')
title('Squared hingeloss');
set(gca, 'YTick',[]);
print(h, '-depsc', 'iris_smhingeloss_1.eps');
print(h, '-dpdf', 'iris_smhingeloss_1.pdf');

%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%

output = iris*w(2,1:end-1)'+w(2,end);
I = find(w(2,1:end-1) ~= 0);
label = cellfun(@(x) (strcmp(x,'Iris-setosa'))*(-1) + ...
                (~strcmp(x,'Iris-setosa'))*(1), iris_label );
error = find(label ~= sign(output))
N = find(label < 0);
P = find(label > 0);


xrange = [floor(min(iris(:,I(1)))) ceil(max(iris(:,I(1))))];
yrange = [floor(min(iris(:,I(2)))) ceil(max(iris(:,I(2))))];
inc = 0.01;
[x, y] = meshgrid(xrange(1):inc:xrange(2), yrange(1):inc:yrange(2));
image_size = size(x);
xy = [x(:) y(:)];
f = xy*w(2,I)'+w(2,end);
idx = (f < 0)*(1) + (f >=0) * 2;
decisionmap = reshape(idx, image_size);

h = clf;
imagesc(xrange,yrange,decisionmap);
hold on;
set(gca,'ydir','normal');

cmap = [1 0.8 0.8; 0.95 1 0.95; 0.9 0.9 1];
colormap(cmap);

plot(iris(N, I(1)), iris(N, I(2)), '+', iris(P, I(1)), ...
       iris(P, I(2)), 'o');
legend('setosa', 'non-setosa', 'Location','NorthOutside','Orientation',...
       'Horizontal')
%line(xlim, ylim,'Color',[1 0 0])
xlabel('Petal length')
ylabel('Petal width')

hold on;
print(h, '-depsc', 'iris_smhingeloss_2.eps');
print(h, '-dpdf', 'iris_smhingeloss_2.pdf');

end

function iris_scale()

r = importdata('test.spambase.scale');

h = clf;

subplot(1,2,1);
hold on;

x = r(:,1)';
topY = r(:,5)';
bottomY = r(:,4)';
y = r(:,3)';
patch([x x(end:-1:1)], [topY bottomY(end:-1:1)],[.8 .8 .8])
plot(x,y, '-','LineWidth',2);
plot(x,r(:,6)', '--','LineWidth',2);
%legend('','MP', 'All');

xlabel('\mu')
ylabel('Accuracy')
xlim([0,11])
ylim([0.7,1]);

subplot(1,2,2);
plot(x,r(:,2)', 'LineWidth', 2);
xlim([0,11])
xlabel('\mu')
ylabel('Average number of features')

set(gcf, 'PaperPositionMode', 'manual');
set(gcf, 'PaperUnits', 'inches');
set(gcf, 'PaperPosition', [2 1 8 3.5]);
print(h, '-depsc', 'spambase_2.eps');
print(h, '-dpdf', 'spambase_2.pdf');


end

function X = normalize_std1(X)
    m = mean(X, 1);
    s = std(X, 1);
    s(s == 0) = 1;
    X = bsxfun(@minus,X,m);
    X = bsxfun(@rdivide,X,s);
end
